package com.example.shonm.loginbigear;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class MainActivity extends ActionBarActivity {

    private EditText tvUser;
    private EditText tvPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvUser = (EditText)findViewById(R.id.tv_username);
        tvPwd = (EditText)findViewById(R.id.tv_passwd);
    }

    class Ret  {
        int retCode = -3;
        String strErrCode = "未知错误";
        String strMemId;
    }

    Handler hander = new Handler()  {
        @Override
        public void handleMessage(Message msg)  {
            if (msg.what == 0x123)  {
                Ret ret = (Ret)msg.obj;
                if  (ret.retCode != 0)  {
                    Toast.makeText(MainActivity.this, ret.strErrCode, Toast.LENGTH_LONG).show();
                }
                else  {
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    intent.putExtra("MemId", ret.strMemId);
                    startActivity(intent);
                }
            }
        }
    };

    public void onBtnLogin(View v)  {
        new Thread()  {
            public void  run()  {
                Ret ret = login(tvUser.getText().toString(), tvPwd.getText().toString());
                Message msg = new Message();
                msg.what = 0x123;
                msg.obj = (Object)ret;
                hander.sendMessage(msg);
            }
        }.start();
    }
    private Ret login(String strUser, String strPwd)  {
        Ret ret = new Ret();
        try  {
            Map<String, String> map = new HashMap<String, String>();
            map.put("MemberName", strUser);
            map.put("Pwd", strPwd);
            Connection.Response response = Jsoup.connect("http://www.bigear.cn/login.html")
                    .data(map).method(Method.POST).execute();
            Document doc = response.parse();
            String sf = response.cookie("sessionFund");
            String id = response.cookie("sessionMemberID");
            Elements result = doc.getElementsByAttributeValue("color", "red");

            if (result.toString().contains("成功登录大耳朵"))  {
                ret.retCode = 0;
                ret.strMemId = id;
            }
            else {
                ret.retCode = 1;
                ret.strErrCode = result.text();
            }
        }
        catch (Exception e)  {
            e.printStackTrace();
            ret.retCode = -1;
            ret.strErrCode = e.toString();
        }
        finally {
            return ret;
        }
    }
}
